package com.zzb.io;

import java.io.*;

public class MyIO {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("a.txt");
        fw.write("万息教育");
        fw.flush();
        fw.close();
    }
}

class Test01 {
    public static void main(String[] args) throws IOException {
        FileWriter fwl = new FileWriter("a.txt");
        fwl.write("中国加油!");
        fwl.close();
        FileWriter fw2 = new FileWriter("a.txt", true);
        fw2.write("哈哈呵哈哈!");
        fw2.close();
    }

}

class User implements Serializable {
    private int age;
    private String name;

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }

    //省略get/set方法

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

class Test02 {
    public static void main(String[] args) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
            oos.writeObject(new User(12, "kkk"));
            oos.writeObject(new User(189, "khghk"));
            oos.close();
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
            Object user;

            while ((user = ois.readObject()) != null) {
                System.out.println(user);
            }
            ois.close();

        } catch (IOException | ClassNotFoundException ignored) {
        }
    }
}
